﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLBH.DataModel;
using DevComponents.DotNetBar;
using QLBH.Model.Controller;
using System.Threading;
using PluginFramework.Common;
using QLBH.Category.Presenter;
using QLBH.FeatureBase.Modules.Category;
using QLBH.FeatureBase;
using QLBH.Base;
using QLBH.ModelInterface;

namespace QLBH.Category
{
    [ExtensionOf(typeof(ProviderManagementToolExtensionPoint))]
    public class QLNhaCungCapTool : BaseView, IProviderManagementTool
    {
        public QLNhaCungCapTool()
            :base()
        {

        }
        public FormViewBase GetView(object arg)
        {
            return new QLNhaCungCapTool();
        }
        public override void AddNew(object sender, EventArgs e)
        {
            try
            {
                AddNhaCungCapDlg addnew = new AddNhaCungCapDlg();
                if (addnew.ShowDialog() == DialogResult.OK)
                {
                    RefreshData(sender, e);
                }
                base.AddNew(sender, e);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "QL Nhà Cung Cấp");
                return;
            }

        }
        public override void EditItem(object sender, EventArgs e)
        {
            try
            {
                AddNhaCungCapDlg editItem = new AddNhaCungCapDlg();
                editItem.DlgType = eDlgType.EDIT;

                var nccModel = DataGridView.SelectedRows[0].DataBoundItem as IQLNCCModel;

                var model = nccModel.ToEntity();
                editItem.Model = model;
                if (editItem.ShowDialog() == DialogResult.OK)
                {
                    RefreshData(sender, e);
                }
                base.EditItem(sender, e);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "QL Nhà Cung Cấp");
                return;
            }

        }
        public override void DeleteItem(object sender, EventArgs e)
        {
            try
            {
                var nccModel = DataGridView.SelectedRows[0].DataBoundItem as IQLNCCModel;
                var model = nccModel.ToEntity();
                NCCController.GetInstance().Delete(model);
                RefreshData(sender, e);
                base.DeleteItem(sender, e);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "QL Nhà Cung Cấp");
                return;
            }

        }
        public override void RefreshData(object sender, EventArgs e)
        {
            base.RefreshData(sender, e);
            LoadDSNCC();
        }
        public override void BuildSearchMenu()
        {
            ButtonItem btnSearchByID = new ButtonItem("btnSearchByID", "Tìm theo Mã NCC");
            ButtonItem btnSearchByName = new ButtonItem("btnSearchByName", "Tìm theo Tên NCC");
            ButtonItem btnSearchByNameUser = new ButtonItem("btnSearchByNameUser", "Tìm theo tên người đại diện");


            BtnSearch.SubItems.Add(btnSearchByID);
            BtnSearch.SubItems.Add(btnSearchByName);
            BtnSearch.SubItems.Add(btnSearchByNameUser);


            btnSearchByID.Click += btnSearchByID_Click;

            btnSearchByName.Click += btnSearchByName_Click;

            btnSearchByNameUser.Click += btnSearchByNameUser_Click;

            base.BuildSearchMenu();
        }

        void btnSearchByNameUser_Click(object sender, EventArgs e)
        {
            List<NHACUNGCAP> source = (List<NHACUNGCAP>)DataSource;

            source = source.Where(m => m.NGUOIDAIDIEN.Contains(TbxSearchInput.ControlText)).ToList();

            DataGridView.DataSource = source;
        }
        void btnSearchByName_Click(object sender, EventArgs e)
        {
            List<IQLNCCModel> source = (List<IQLNCCModel>)DataSource;

            source = source.Where(m => m.TENNCC.Contains(TbxSearchInput.ControlText)).ToList();

            DataGridView.DataSource = source;
        }

        void btnSearchByID_Click(object sender, EventArgs e)
        {
            List<IQLNCCModel> source = (List<IQLNCCModel>)DataSource;

            source = source.Where(m => m.MANCC.Contains(TbxSearchInput.ControlText)).ToList();

            DataGridView.DataSource = source;
        }
        public override void LoadForm(object sender, EventArgs e)
        {
            base.LoadForm(sender, e);

            LoadDSNCC();
        }
        private void LoadDSNCC()
        {
            var presenter = new QLNCCPresenter();
            var items = presenter.GetDSNCC();
            DataSource = items;

            DataGridView.DataSource = DataSource;            

            DataGridView.Columns["MANCC"].HeaderText = "Mã NCC";
            DataGridView.Columns["MANCC"].DisplayIndex = 0;

            DataGridView.Columns["TENNCC"].HeaderText = "Tên NCC";
            DataGridView.Columns["TENNCC"].DisplayIndex = 1;

            DataGridView.Columns["MAST"].HeaderText = "Mã số thuế";
            DataGridView.Columns["MAST"].DisplayIndex = 2;

            DataGridView.Columns["NGUOIDD"].HeaderText = "Người ĐD";
            DataGridView.Columns["NGUOIDD"].DisplayIndex = 3;

            DataGridView.Columns["CHUC_VU"].HeaderText = "Chức Vụ";
            DataGridView.Columns["CHUC_VU"].DisplayIndex = 4;

            DataGridView.Columns["DIACHI"].HeaderText = "Địa Chỉ";
            DataGridView.Columns["DIACHI"].DisplayIndex = 5;

            DataGridView.Columns["DIENTHOAI"].HeaderText = "SĐT";
            DataGridView.Columns["DIENTHOAI"].DisplayIndex = 6;

            DataGridView.Columns["DIDONG"].HeaderText = "Di động";
            DataGridView.Columns["DIDONG"].DisplayIndex = 7;

            DataGridView.Columns["FAX"].HeaderText = "Fax";
            DataGridView.Columns["FAX"].DisplayIndex = 8;

            DataGridView.Columns["EMAIL"].HeaderText = "Email";
            DataGridView.Columns["EMAIL"].DisplayIndex = 9;

            DataGridView.Columns["WEBSITE"].HeaderText = "Website";
            DataGridView.Columns["WEBSITE"].DisplayIndex = 10;

            DataGridView.Columns["GHICHU"].HeaderText = "Ghi Chú";
            DataGridView.Columns["GHICHU"].DisplayIndex = 11;

            DataGridView.Columns["NGAYTAO"].HeaderText = "Ngày Tạo";
            DataGridView.Columns["NGAYTAO"].DisplayIndex = 12;
            DataGridView.Columns["NGAYTAO"].DefaultCellStyle.Format =
                Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern;

            DataGridView.Columns["NCCID"].Visible = false;
            DataGridView.Columns["NCCID"].DisplayIndex = 13;
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // QLNhaCungCapTool
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
            this.Name = "QLNhaCungCapTool";
            this.Size = new System.Drawing.Size(1226, 846);
            this.ResumeLayout(false);

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
